package wangm.repository;

import wangm.bean.Activity;
import wangm.bean.ActivtiyQueryParameter;
import wangm.bean.Pagination;
import wangm.util.DBOperatorComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动repository
 *
 * @author zk
 * @since 2018/5/11
 */
@Repository
public class ActivityRepository {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(ActivityRepository.class);
    /**
     * 当前表名
     */
    public static final String thisTableName = "T_ACTIVITY";
    /**
     * db操作
     */
    @Autowired
    private DBOperatorComponent dbOperatorComponent;
    /**
     * 添加活动
     *
     * @param activity 活动
     */
    public void add(Activity activity){
        String sql = "insert into " + thisTableName + " (id,name,abstractContent,picUrl,content,type,status,beginTime,endTime,permit,createTime) " +
                "values(?,?,?,?,?,?,?,?,?,?,?)";
        List parameter = new ArrayList();
        parameter.add(activity.getId());
        parameter.add(activity.getName());
        parameter.add(activity.getAbstractContent());
        parameter.add(activity.getPicUrl());
        parameter.add(activity.getContent());
        parameter.add(activity.getType());
        parameter.add(activity.getStatus());
        parameter.add(activity.getBeginTime());
        parameter.add(activity.getEndTime());
        parameter.add(activity.getPermit());
        parameter.add(activity.getCreateTime());
        try {
            dbOperatorComponent.update(sql,parameter.toArray());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 获取分页列表
     *
     * @param queryParameter 查询参数
     * @param pagination 分页数据
     * @return list<Activity></>
     */
    public List<Activity> getList(ActivtiyQueryParameter queryParameter, Pagination<Activity> pagination){
        String sql = "select * from " + thisTableName;
        try {
            return dbOperatorComponent.getBeanList(Activity.class,sql,pagination);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 更新活动
     *
     * @param activity activity
     */
    public void update(Activity activity){
        List parameter = new ArrayList();
        StringBuffer sqlBuffer = new StringBuffer("update " + thisTableName + " set id = ? ");
        parameter.add(activity.getId());

        if(!StringUtils.isEmpty(activity.getName())){
            sqlBuffer.append(" ,name = ?");
            parameter.add(activity.getName());
        }

        if(!StringUtils.isEmpty(activity.getAbstractContent())){
            sqlBuffer.append(", abstractContent = ?");
            parameter.add(activity.getAbstractContent());
        }

        if(!StringUtils.isEmpty(activity.getPicUrl())){
            sqlBuffer.append(", picUrl = ?");
            parameter.add(activity.getPicUrl());
        }

        if(!StringUtils.isEmpty(activity.getContent())){
            sqlBuffer.append(", content = ?");
            parameter.add(activity.getContent());
        }

        if(!StringUtils.isEmpty(activity.getType())){
            sqlBuffer.append(", type = ?");
            parameter.add(activity.getType());
        }

        if(!StringUtils.isEmpty(activity.getStatus())){
            sqlBuffer.append(", status = ?");
            parameter.add(activity.getStatus());
        }

        if(!StringUtils.isEmpty(activity.getBeginTime())){
            sqlBuffer.append(", beginTime = ?");
            parameter.add(activity.getBeginTime());
        }

        if(!StringUtils.isEmpty(activity.getEndTime())){
            sqlBuffer.append(", endTime = ?");
            parameter.add(activity.getEndTime());
        }

        if(!StringUtils.isEmpty(activity.getPermit())){
            sqlBuffer.append(", permit = ?");
            parameter.add(activity.getPermit());
        }

        sqlBuffer.append(" where id = ?");
        parameter.add(activity.getId());

        try {
            dbOperatorComponent.update(sqlBuffer.toString(),parameter.toArray());
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 删除指定活动
     *
     * @param id id
     */
    public void remove(String id){
        String sql = "delete from " + thisTableName + " where id = ?";
        try {
            dbOperatorComponent.update(sql,id);
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 获取指定id的活动
     *
     * @param id id
     * @return bean
     */
    public Activity get(String id){
        try {
            return dbOperatorComponent.getById(id,thisTableName,Activity.class);
        } catch (Exception e) {
            return null;
        }
    }
}
